package com.jade.cloud.store.service.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.store.dao.NormDao;
import com.jade.zsy.store.entity.Norm;
import com.jade.zsy.store.service.NormFacade;
import com.jade.zsy.store.vo.NormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version 1.0--Created by wq on 2017/7/1.
 * @Description:
 */
@Service("normFacadeImpl")
public class NormFacadeImpl extends BaseFacadeImpl implements NormFacade {

    @Autowired
    private NormDao normDao;

    @Override
    @Transactional
    public void save(Object entity) throws DbException {
        NormVo vo = (NormVo)entity;
        Norm norm = null;
        try {
            norm = CopyBean.copy(vo,Norm.class);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        super.save(norm);

    }

    @Transactional
    public void delete(long... ids) throws DbException {
        super.delete(Norm.class,ids);
    }

    @Transactional
    public void update(NormVo normVo)throws DbException,IllegalAccessException,InstantiationException{
        Norm norm = CopyBean.copy(normVo,Norm.class);
        super.update(norm);
    }

    public NormVo query(long NormVo)throws DbException,IllegalAccessException,InstantiationException{
        PagingList<Norm> list =  super.queryEntity(Norm.class,new Criteria().add(Restrictions.eq("NormVo",NormVo)));
        if(!list.isEmpty()) {
            List<NormVo> list1 = CopyBean.copy(list,NormVo.class);
            return list1.get(0);
        }
        return null;
    }

    public PagingResult queryEntity(Object entity, int page, int size, String keys)
            throws DbException, IllegalAccessException, InstantiationException{
        NormVo normVo = (NormVo)entity;
        Criteria criteria = new Criteria((page - 1) * size, size);
        PagingList<Norm> list = normDao.queryEntity(Norm.class,criteria);
        PagingResult pagingResult = PagingResult.getResult(CopyBean.copy(list,normVo.getClass()),list.getTotalCount(),page,size);
        return pagingResult;
    }
}
